package code.generate.base;

import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/3/23 12:47
 */
public abstract class AbstractMojo extends org.apache.maven.plugin.AbstractMojo {

    public static String CODE_DIR;

    public static String BASE_DIR;

    /**
     * Maven Project. 使用插件的maven项目
     */
    @Parameter(property = "project", required = true, readonly = true)
    protected MavenProject project;


    /**
     * 获得setting文件对象
     */
    @Component
    protected Settings settings;

    protected Properties getProperties(String fileName) {
        File basedir = project.getBasedir();
        BASE_DIR = basedir.getAbsolutePath();
        CODE_DIR = basedir.getAbsolutePath() + "/src/main/resources/" + fileName;
        if (basedir.exists()) {
            File file = new File(CODE_DIR);
            if (!file.exists()) {
                return null;
            }

            Properties properties = new Properties();
            try (FileInputStream in = new FileInputStream(file)) {
                properties.load(in);
                return properties;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;

    }

}
